package com.gegewa.topic.jdata.sameagain;

import com.csvreader.CsvReader;
import com.csvreader.CsvWriter;
import lombok.Data;

import java.io.IOException;
import java.nio.charset.Charset;
import java.text.SimpleDateFormat;
import java.util.*;

@Data
public class DataLoader {

    public Map<String, Set<String>> loadData() {
        String filePath1 = "D:\\\\JData_内部赛\\JData_Action_0301_0315.csv";
        String filePath2 = "D:\\\\JData_内部赛\\JData_Action_0316_0331.csv";
        String filePath3 = "D:\\\\JData_内部赛\\JData_Action_0401_0415.csv";

        String filePath111 = "D:\\\\JData_外部赛\\JData_Action_0301_0315.csv";
        String filePath222 = "D:\\\\JData_外部赛\\JData_Action_201604.csv";
        String filePath333 = "D:\\\\JData_外部赛\\JData_Action_0401_0415.csv";


        Set<String> productSet = getProductSet();

        Map<String, Set<String>> historyMap = new HashMap<>(60000);

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String nowData = simpleDateFormat.format(new Date());
        System.out.println("加载文件开始时间：" + nowData);

        readPurchaseHistoryList(filePath222, productSet, historyMap);

        nowData = simpleDateFormat.format(new Date());
        System.out.println("加载文件结束时间：" + nowData + "，historyMap.size()=" + historyMap.size());
        return historyMap;
    }

    public void readPurchaseHistoryList(String filePath, Set<String> productSet, Map<String, Set<String>> historyMap) {
        try {
            // 创建CSV读对象
            CsvReader csvReader = new CsvReader(filePath);

            // 读表头
            csvReader.readHeaders();
            int i = 0;
            while (csvReader.readRecord()) {
//                if (++i < 3000000) {
//                    continue;
//                }
                if (++i > 3000000) {
                    break;
                }
                String userId = csvReader.get("user_id");
                String skuId = csvReader.get("sku_id");
                addMap(userId, skuId, productSet, historyMap);
            }

        } catch (IOException e) {
            e.printStackTrace();
            System.out.println("读取" + filePath + "出错");
        }
    }

    private void addMap(String userId, String skuId, Set<String> productSet, Map<String, Set<String>> historyMap) {
        if (!productSet.contains(skuId)) {
            return;
        }
        if (historyMap.containsKey(userId)) {
            Set set = historyMap.get(userId);
            set.add(skuId);
            historyMap.put(userId, set);
        } else {
            Set<String> set = new HashSet<>();
            set.add(skuId);
            historyMap.put(userId, set);
        }
    }





    public void loadUserIdSkuIdWeight(Set<String> productSet, Map<String, Integer> userIdHistoryMap, Map<String, Integer> skuIdHistoryMap) {
        String filePath1 = "D:\\\\JData_内部赛\\JData_Action_0301_0315.csv";
        String filePath2 = "D:\\\\JData_内部赛\\JData_Action_0316_0331.csv";
        String filePath3 = "D:\\\\JData_内部赛\\JData_Action_0401_0415.csv";

//        String filePath111 = "D:\\\\JData_外部赛\\JData_Action_201602.csv";
//        String filePath222 = "D:\\\\JData_外部赛\\JData_Action_201603.csv";
//        String filePath333 = "D:\\\\JData_外部赛\\JData_Action_201604.csv";

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String nowData = simpleDateFormat.format(new Date());

        System.out.println("加载文件开始时间：" + nowData);

//        readUserIdSkuIdWeight(filePath1, productSet, userIdHistoryMap, skuIdHistoryMap);
//        nowData = simpleDateFormat.format(new Date());
//        System.out.println("加载第一个文件结束时间：" + nowData + "，userIdHistoryMap.size()=" + userIdHistoryMap.size() + "，skuIdHistoryMap.size()=" + skuIdHistoryMap.size());
//
//        readUserIdSkuIdWeight(filePath2, productSet, userIdHistoryMap, skuIdHistoryMap);
//        nowData = simpleDateFormat.format(new Date());
//        System.out.println("加载第二个文件结束时间：" + nowData + "，userIdHistoryMap.size()=" + userIdHistoryMap.size() + "，skuIdHistoryMap.size()=" + skuIdHistoryMap.size());

        readUserIdSkuIdWeight(filePath3, productSet, userIdHistoryMap, skuIdHistoryMap);
        nowData = simpleDateFormat.format(new Date());
        System.out.println("加载第三个文件结束时间：" + nowData + "，userIdHistoryMap.size()=" + userIdHistoryMap.size() + "，skuIdHistoryMap.size()=" + skuIdHistoryMap.size());
    }

    public void readUserIdSkuIdWeight(String filePath, Set<String> productSet, Map<String, Integer> userIdHistoryMap, Map<String, Integer> skuIdHistoryMap) {
        try {
            // 创建CSV读对象
            CsvReader csvReader = new CsvReader(filePath);

            // 读表头
            csvReader.readHeaders();
            int i = 0;
            while (csvReader.readRecord()) {
                if (++i < 4100000) {
                    continue;
                }
                if (++i > 43000000) {
                    break;
                }
                addWeightToMap(csvReader.get("user_id"), userIdHistoryMap);
                String skuId = csvReader.get("sku_id");
                if (productSet.contains(skuId)) {
                    addWeightToMap(skuId, skuIdHistoryMap);
                }
            }

            System.out.println("读取了 " + i + " 条购物记录");

        } catch (IOException e) {
            e.printStackTrace();
            System.out.println("读取" + filePath + "出错");
        }
    }

    private void addWeightToMap(String key, Map<String, Integer> map) {
        if (map.containsKey(key)) {
            map.put(key, map.get(key) + 1);
        } else {
            map.put(key, 1);
        }
    }

    public Set<String> getProductSet() {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String nowData = simpleDateFormat.format(new Date());
        System.out.println("加载Product开始时间：" + nowData);

        Set<String> productSet = new HashSet<>(12500);
        String filePath = "D:\\\\JData_内部赛\\JData_Product.csv";
        try {
            // 创建CSV读对象
            CsvReader csvReader = new CsvReader(filePath);

            // 读表头
            csvReader.readHeaders();
            while (csvReader.readRecord()){
                String skuId = csvReader.get("sku_id");
                productSet.add(skuId);
            }

        } catch (IOException e) {
            e.printStackTrace();
            System.out.println("读取" + filePath + "出错");
        }
        nowData = simpleDateFormat.format(new Date());
        System.out.println("加载Product结束时间：" + nowData + "，productSet.size()=" + productSet.size());
        return productSet;
    }

    public void write(List<String[]> forecastList){
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String nowData = simpleDateFormat.format(new Date());
        System.out.println("写结果文件开始时间：" + nowData);

        SimpleDateFormat writeTime = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss");
        String filename = writeTime.format(new Date());

        String filePath = "D:\\\\JData_内部赛\\" + filename + ".csv";

        try {
            // 创建CSV写对象
            CsvWriter csvWriter = new CsvWriter(filePath,',', Charset.forName("UTF-8"));

            // 写表头
            String[] headers = {"user_id","sku_id"};
            csvWriter.writeRecord(headers);
            for (int i = 0, size = forecastList.size(); i < size; i++) {
                csvWriter.writeRecord(forecastList.get(i));
            }
            csvWriter.close();

        } catch (IOException e) {
            e.printStackTrace();
            System.out.println("写入" + filePath + "出错");
        }
        nowData = simpleDateFormat.format(new Date());
        System.out.println("写结果文件结束时间：" + nowData);
    }

    public void write(List<String> userIdList, String skuId){
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String nowData = simpleDateFormat.format(new Date());
        System.out.println("写结果文件开始时间：" + nowData);

        SimpleDateFormat writeTime = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss");
        String filename = writeTime.format(new Date());

        String filePath = "D:\\\\JData_内部赛\\" + filename + ".csv";

        try {
            // 创建CSV写对象
            CsvWriter csvWriter = new CsvWriter(filePath,',', Charset.forName("UTF-8"));

            // 写表头
            String[] headers = {"user_id","sku_id"};
            csvWriter.writeRecord(headers);
            for (int i = 0, size = userIdList.size(); i < size; i++) {
                csvWriter.writeRecord(new String[] {userIdList.get(i), skuId});
            }
            csvWriter.close();

        } catch (IOException e) {
            e.printStackTrace();
            System.out.println("写入" + filePath + "出错");
        }
        nowData = simpleDateFormat.format(new Date());
        System.out.println("写结果文件结束时间：" + nowData);
    }

    public void readFirstAndSecond(Map<String, String> userSkuMap, List<String> userList) {
        String firstPath = "D:\\\\JData_内部赛\\2017-04-13-11-27-37.csv";
        String secondPath = "D:\\\\JData_内部赛\\2017-04-13-11-13-08.csv";

        try {
            // 创建CSV读对象
            CsvReader csvReader = new CsvReader(firstPath);

            // 读表头
            csvReader.readHeaders();
            while (csvReader.readRecord()) {
                userList.add(csvReader.get("user_id"));
            }


        } catch (IOException e) {
            e.printStackTrace();
            System.out.println("读取" + firstPath + "出错");
        }
        try {
            // 创建CSV读对象
            CsvReader csvReader = new CsvReader(secondPath);

            // 读表头
            csvReader.readHeaders();
            while (csvReader.readRecord()) {
                userSkuMap.put(csvReader.get("user_id"), csvReader.get("sku_id"));
            }

        } catch (IOException e) {
            e.printStackTrace();
            System.out.println("读取" + secondPath + "出错");
        }
    }

}
